-- Copyright (C) 2013 wgm.china@gmail.com (王光明)
--vss.list.method为所有list方法的操作函数
--date:2013-12-11

local strfind = string.find
local strsub  = string.sub
local strbyte = string.byte
local strrep  = string.rep
local strchar = string.char
local strrep  = string.rep
local strlen  = string.len
local type    = type
local pairs   = pairs
local tonumber= tonumber 
local tb_insert = table.insert

-------1定义公共组件(开始)---------------
local list_util           = require("gcs.list.utils")
local config_table 		  = ngx.shared.gcs_config  					--定义gcs_config缓存
-------1定义公共组件（结束）----------------

module(...)
local version = '1.1'

-------2加载配置文件-----------------------
local config_lists 	         = config_table:get("lists")
local config_list_push_lr    = config_table:get("list_push_lr") 
local config_list_pop_lr     = config_table:get("list_pop_lr") 


-------2加载配置文件（结束）----------------

strsplit = function(s, p)
    local rt= {}
    string.gsub(s, '[^'..p..']+', function(w) table.insert(rt,w) end )
    return rt
end

--把数据加入内存，并把id加入队列
function push_list(lr,list_name,id,data)
	local set_data = {}
	for key,value in pairs(data) do
		set_data[id..":"..key] = value
	end
	local res,err = list_util.set(set_data)
	if not res then
		 return nil,err
	end
	local res,err = list_util.push(lr,list_name,id,data)
	return res,err
end

--从队列中取出id，并把id相对应的数据一并取出返回
function pop_list(lr,list_name,id,data)
	local id,err = list_util.pop(lr,list_name)
	if not id then
		 return nil,err
	end
	if id == ngx.null then
		return list_name,0
	end
	local get_data = {}
	for _,key in pairs(data) do
		tb_insert(get_data,id..":"..key)
	end
	local tb_res,err = list_util.get(get_data)
	if not tb_res then 
		return nil,err
	end
	return list_name,id,tb_res
end


--把id保存到队列中
function push(lr,list_name,id)
	local res,err = list_util.push(lr,list_name,id)
	return res,err
end


--从队列中取id
function pop(lr,list_name)
	local res,err = list_util.pop(lr,list_name)
	return res,err
end

--return version 中保存的版本号
function get_version()
	return version
end

--取队列的总数,参数为队列名或队列名table
--已经完成，并测试成功
function list_count(list_name)
	if not list_name then
		return nil,"list_name is nil"
	end
	local res,err
	local tb_res = {}
	if type(list_name) == "string" then
		res,err = list_util.count(list_name)
		return res,err
	elseif type(list_name) == "table" then
		for _,list in pairs(list_name) do
			res,err = list_util.count(list)
			if not res then
				return nil,err
			else
				tb_res[list] = res
			end	
		end
		return tb_res,err
	else
		return nil,"list_name is fails"
	end	
end

--从缓存中取数据,参数为key或keys
--已经完成，并测试成功
--如果缓存内容为空则返回""
function get_data(data)
	local tb_data = {}
	if not data then
	 	return nil,"The data must be greater than or equal to 1"
	end	 
	if type(data) ~="table" then
		tb_insert(tb_data,data)
	else
		tb_data = data
	end
	local res,err = list_util.get(tb_data)
	return res,err
end

--从缓存中取数据,参数为key或keys
--已经完成，并测试成功
--如果缓存内容为空则返回0
function get_data1(data)
	local tb_data = {}
	if not data then
	 	return nil,"The data must be greater than or equal to 1"
	end	 
	if type(data) ~="table" then
		tb_insert(tb_data,data)
	else
		tb_data = data
	end
	local res,err = list_util.get1(tb_data)
	return res,err
end

--把数据保存到缓存中,参数为key=value的table
--已经完成，并测试成功
function set_data(data)
	local tb_data = {}
	if type(data) ~="table" then
		return nil,"Parameter must be table format"
	else
		tb_data = data
	end
	local res,err = list_util.set(tb_data)
	return res,err
end

--把数据保存到缓存中,参数为key=value的table
--已经完成，并测试成功
function del_data(data)
	local tb_data = {}
	if type(data) ~="table" then
		return nil,"Parameter must be table format"
	else
		tb_data = data
	end
	local res,err = list_util.del(tb_data)
	return res,err
end


--把缓存中的记数器增加指定值，参数为key=value的table
--已经完成，并测试成功
function incr_data(data)
	local tb_data = {}
	if type(data) ~="table" then
		return nil,"Parameter must be table format"
	else
		tb_data = data
	end
	local res,err = list_util.incr(tb_data)
	return res,err
end



function exists(data)
  local tb_data = {}
  if not data then
    return nil,"The data must be greater than or equal to 1"
  end  
  if type(data) ~="table" then
    tb_insert(tb_data,data)
  else
    tb_data = data
  end
  local res,err = list_util.exists(tb_data)
  return res,err
end








--[[
--取队列名称列表
function get_lists()
	local list_name = {}
	if config_lists then
		list_name = strsplit(config_lists)
	end	
	return list_name
end

--根据配置的要求进行数据保存和队列push
function push_data(list_name,id,data)
	local lr = config_list_push_lr or "l"
	local res,err = push_list(lr,list_name,id,data)	
end
--]]
